package tt.dz.dao.ext.system;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import tt.dz.dao.base.OmsCommonDao;
import tt.dz.entity.exception.OmsDaoException;
import tt.dz.entity.system.OmsOperationSpList;
import tt.dz.util.annotation.Table;
import tt.dz.entity.system.OmsOperationBranchSpList;
/**
 * <p>该类主要用于查询运营商分支公司数据信息</p>
 * 
 *  <p>因为有特定查询需求，所以加入新的查询信息</p>
 * @author huanghao
 *
 */
@Repository
@Table(name = "oms_operation_branch_sp_list")
public class OmsOperationBranchSpListDao extends OmsCommonDao<OmsOperationBranchSpList>
{
	/**
	 * select 
		oper_merchant_branch_id,
		branch.oper_merchant_id,
		city_codes,
		branch.oper_name as b_oper_name,
		branch.tel as b_tel,
		branch.valid_flag as b_valid_flag,
		branch.create_time as b_create_time,
		branch.create_oper_id as b_create_oper_id,
		branch.update_time as b_update_time,
		branch.update_oper_id as b_update_oper_id,
		super_flag,
		communication_no,
		ot.oper_name as oper_name,
		ot.tel as tel,
		ot.valid_flag as valid_flag,
		ot.create_time as create_time,
		ot.create_oper_id as create_oper_id,
		ot.update_oper_id as update_oper_id,
		ot.update_time as update_time
		 from oms_operation_branch_sp_list branch LEFT JOIN oms_operation_sp_list ot
		on branch.oper_merchant_id = ot.oper_merchant_id 
		where branch.oper_merchant_id = ?
		查询单个运营商分公司，并级联查询出主运营商
	 * @param id
	 * @return
	 * @throws OmsDaoException
	 */
	public OmsOperationBranchSpList findOmbWithId(String id)throws OmsDaoException
	{
		String sql = "select oper_merchant_branch_id,branch.oper_merchant_id,city_codes,branch.oper_name as b_oper_name,branch.tel as b_tel,branch.valid_flag as b_valid_flag,branch.create_time as b_create_time,branch.create_oper_id as b_create_oper_id,branch.update_time as b_update_time,branch.update_oper_id as b_update_oper_id,super_flag,communication_no,ot.oper_name as oper_name,ot.tel as tel,ot.valid_flag as valid_flag,ot.create_time as create_time,ot.create_oper_id as create_oper_id,ot.update_oper_id as update_oper_id,ot.update_time as update_time from oms_operation_branch_sp_list branch LEFT JOIN oms_operation_sp_list ot on branch.oper_merchant_id = ot.oper_merchant_id where branch.oper_merchant_id = ?";
		try{
			return this.getJdbcTemplate().queryForObject(sql, new RowMapper<OmsOperationBranchSpList>() 
			{
	
				@Override
				public OmsOperationBranchSpList mapRow(ResultSet arg0, int arg1) throws SQLException 
				{
					OmsOperationBranchSpList omb = new OmsOperationBranchSpList();
					omb.setOper_merchant_branch_id(arg0.getString("oper_merchant_branch_id"));
					omb.setOper_merchant_id(arg0.getString("oper_merchant_id"));
					omb.setOper_name(arg0.getString("b_oper_name"));
					omb.setCity_codes(arg0.getString("city_codes"));
					omb.setTel(arg0.getString("b_tel"));
					omb.setValid_flag(arg0.getInt("b_valid_flag"));
					omb.setUpdate_oper_id(arg0.getString("b_update_oper_id"));
					omb.setUpdate_time(arg0.getDate("b_update_time"));
					omb.setCreate_oper_id(arg0.getString("b_create_oper_id"));
					omb.setCreate_time(arg0.getDate("b_create_time"));
					
					OmsOperationSpList om = new OmsOperationSpList();
					om.setOper_merchant_id(arg0.getString("oper_merchant_id"));
					om.setCommunication_no(arg0.getString("communication_no"));
					om.setOper_name(arg0.getString("oper_name"));
					om.setTel(arg0.getString("tel"));
				
					om.setValid_flag(arg0.getInt("valid_flag"));
					om.setUpdate_oper_id(arg0.getString("update_oper_id"));
					om.setUpdate_time(arg0.getDate("update_time"));
					om.setCreate_oper_id(arg0.getString("create_oper_id"));
					om.setCreate_time(arg0.getDate("create_time"));
					
					omb.setOperMerchant(om);
					return omb;
					
				}
			}, id);
		}
		catch(Exception e)
		{
			throw new OmsDaoException(e);
		}
	}
	/**
	 * 根据主运营商Id逻辑上传相应的子运营商.
	 * @param operId
	 * @throws OmsDaoException
	 */
	public void updateBranchByOperId(String operId)throws OmsDaoException
	{
		int flag =  0;
		try
		{
			flag = this.getJdbcTemplate().update("update oms_operation_branch_sp_list set valid_flag = 0 where oper_merchant_id = ?", operId);
		}
		catch(Exception e)
		{
			throw new OmsDaoException(e.getMessage(),e);
		}
		if (flag < 0)
		{
			throw new OmsDaoException("update fail");
		}
	}
}
